package org.example.common.datasource.utils;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.common.core.utils.DateUtils;
import org.example.common.core.utils.page.Pager;
import org.example.common.core.utils.page.SearchParam;
import org.example.common.core.utils.page.TemplateUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

public class SearchParamsHelper {

    /**
     * 是否加载 查询条件
     *
     * @param cnd
     * @param k
     * @param v
     * @return
     */
    private static boolean idLoadCnd(String cnd, String k, Object v) {
        return k.startsWith(cnd) && null != v && v.toString().length() > 0;
    }

    /**
     * 加载 搜索条件
     *
     * @param pager
     * @param wrapper
     */
    public static void loadSearchParam(Pager pager, QueryWrapper<?> wrapper) {
        if (pager != null && pager.getSearchParams() != null && pager.getSearchParams().size() > 0) {
            Map<String, Object> searchParams = pager.getSearchParams();
            if (null != searchParams) {
                searchParams.forEach((k,v) -> {
                    if (idLoadCnd(SearchParam.SEARCH_EQ, k, v)) {
                        wrapper.eq(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_EQ)[1]), String.valueOf(v).trim());
                    } else if (idLoadCnd(SearchParam.SEARCH_GE, k, v)) {
                        wrapper.ge(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_GE)[1]), v);
                    } else if (idLoadCnd(SearchParam.SEARCH_LE, k, v)) {
                        wrapper.le(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_LE)[1]), v);
                    }else if (idLoadCnd(SearchParam.SEARCH_RIGHT_LIKE, k,v)) {
                        wrapper.likeRight(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_RIGHT_LIKE)[1]), String.valueOf(v).trim());
                    }
                    else if (idLoadCnd(SearchParam.SEARCH_LIKE, k, v)) {
                        wrapper.like(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_LIKE)[1]), String.valueOf(v).trim());
                    } else if (idLoadCnd(SearchParam.SEARCH_DT_GE, k, v)) {//时间大于小于
                        wrapper.ge(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_GE)[1]), v);
                    } else if (idLoadCnd(SearchParam.SEARCH_DT_LE, k, v)) {//时间大于小于
                        wrapper.le(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_LE)[1]), v);
                    } else if (idLoadCnd(SearchParam.SEARCH_IN, k, v)) {//包含
                        List data=(ArrayList) v;
                        wrapper.in(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_IN)[1]), data.toArray());
                    }else if (idLoadCnd(SearchParam.SEARCH_NOTIN, k, v)) {//不包含
                        List data=(ArrayList) v;
                        wrapper.notIn(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_NOTIN)[1]), data.toArray());
                    }
                    else if (idLoadCnd(SearchParam.SEARCH_DT_BETWEEN, k, v)) {//时间区间
                        List dts = (ArrayList) v;
                        dts.set(0, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.dateTime(DateUtils.YYYY_MM_DD, dts.get(0).toString())));
                        dts.set(1, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.dateTime(DateUtils.YYYY_MM_DD, dts.get(1).toString())));
                        wrapper.between(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_BETWEEN)[1]),
                                String.format("%s%s", dts.get(0), " 00:00:00"),
                                String.format("%s%s", dts.get(1), " 23:59:59"));
                    }
                    else if (idLoadCnd(SearchParam.SEARCH_DTS_BETWEEN, k, v)) {//时间区间
                        List dts = (ArrayList) v;
                        wrapper.between(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_BETWEEN)[1]),
                                dts.get(0).toString(), dts.get(1).toString());
                    }
                });
            }
        }
    }


    /**
     * 加载 搜索条件(别名)
     *
     * @param pager
     * @param wrapper
     */
    public static void loadSearchParam(Pager pager, QueryWrapper<?> wrapper,Map<String,String> aliasMap) {
        if (pager != null && pager.getSearchParams() != null && pager.getSearchParams().size() > 0) {
            Map<String, Object> searchParams = pager.getSearchParams();
            if (null != searchParams) {
                searchParams.forEach((k,v) -> {
                    if (idLoadCnd(SearchParam.SEARCH_EQ, k, v)) {
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_EQ)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_EQ)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        wrapper.eq(key, v);
                    } else if (idLoadCnd(SearchParam.SEARCH_GE, k, v)) {
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_GE)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_GE)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        wrapper.ge(key, v);
                    } else if (idLoadCnd(SearchParam.SEARCH_LE, k, v)) {
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_LE)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_LE)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        wrapper.le(key, v);
                    }  else if (idLoadCnd(SearchParam.SEARCH_RIGHT_LIKE, k,v)) {
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_RIGHT_LIKE)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_RIGHT_LIKE)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        wrapper.likeRight(key, String.valueOf(v).trim());
                    }else if (idLoadCnd(SearchParam.SEARCH_LIKE, k, v)) {
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_LIKE)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_LIKE)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        wrapper.like(key, String.valueOf(v).trim());
                    } else if (idLoadCnd(SearchParam.SEARCH_DT_GE, k, v)) {//时间大于小于
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_GE)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_DT_GE)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        wrapper.ge(key, v);
                    } else if (idLoadCnd(SearchParam.SEARCH_DT_LE, k, v)) {//时间大于小于
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_LE)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_DT_LE)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        wrapper.le(key, v);
                    } else if (idLoadCnd(SearchParam.SEARCH_IN, k, v)) {//包含
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_IN)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_IN)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        List data=(ArrayList) v;
                        wrapper.in(key, data.toArray());
                    }else if (idLoadCnd(SearchParam.SEARCH_NOTIN, k, v)) {//不包含
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_NOTIN)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_NOTIN)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        List data=(ArrayList) v;
                        wrapper.notIn(key, data.toArray());
                    }
                    else if (idLoadCnd(SearchParam.SEARCH_DT_BETWEEN, k, v)) {//时间区间
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_BETWEEN)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_DT_BETWEEN)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        List dts = (ArrayList) v;
                        dts.set(0, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.dateTime(DateUtils.YYYY_MM_DD, dts.get(0).toString())));
                        dts.set(1, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.dateTime(DateUtils.YYYY_MM_DD, dts.get(1).toString())));
                        wrapper.between(key,String.format("%s%s", dts.get(0), " 00:00:00"), String.format("%s%s", dts.get(1), " 23:59:59"));
                    }
                    else if (idLoadCnd(SearchParam.SEARCH_DTS_BETWEEN, k, v)) {//时间区间
                        String key=TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DTS_BETWEEN)[1]);
                        String alias=  aliasMap.get(k.split(SearchParam.SEARCH_DTS_BETWEEN)[1]);
                        if(!ObjectUtil.isEmpty(alias)){
                            key=String.format("%s.%s",alias,key);
                        }
                        List dts = (ArrayList) v;
                        wrapper.between(key,dts.get(0).toString(), dts.get(1).toString());
                    }
                });
            }
        }
    }

    /**
     * 加载 搜索条件
     *
     * @param pager
     * @param wrapper
     * @param function
     */
    public static void loadSearchParam(Pager pager, QueryWrapper wrapper,BiFunction<Page,QueryWrapper, IPage> function){
        SearchParamsHelper.loadSearchParam(pager,wrapper);
        IPage iPage=function.apply(new Page(pager.getPageNum(),pager.getPageSize()),wrapper);
        pager.setRows(iPage.getRecords());
        pager.setTotal(iPage.getTotal());
        pager.setTotalPage(iPage.getPages());
    }

    /**
     * 加载 搜索条件(别名)
     *
     * @param pager
     * @param wrapper
     * @param aliasMap
     * @param function
     */
    public static void loadSearchParam(Pager pager, QueryWrapper wrapper,Map<String,String> aliasMap,BiFunction<Page,QueryWrapper,IPage> function){
        SearchParamsHelper.loadSearchParam(pager,wrapper,aliasMap);
        IPage iPage=function.apply(new Page(pager.getPageNum(),pager.getPageSize()),wrapper);
        pager.setRows(iPage.getRecords());
        pager.setTotal(iPage.getTotal());
        pager.setTotalPage(iPage.getPages());
    }
}

